from django.http import HttpResponse
from django.shortcuts import render
from cookiexx.models import User

# Create your views here.


def set_cookie_handler(request, key, value):
    response = HttpResponse()
    response.set_cookie(key, value, max_age=60*60)
    return response


def get_cookie_handler(request, key):
    value = request.COOKIES.get(key)
    return HttpResponse(value)


def set_session_handler(request, key, value):
    request.session[key] = value
    request.session.set_expiry(60*60)
    return HttpResponse('设置成功')


def get_session_handler(request, key):
    value = request.session.get(key)
    return HttpResponse(value)


def flush_session_handler(request):
    request.session.flush()
    return HttpResponse('flush')


def clear_session_handler(request):
    request.session.clear()
    return HttpResponse('clear')


def form_handler(request):
    return render(request, './form.html')


def form_post_handler(request):
    username = request.POST.get('username')
    hobbys = request.POST.getlist('hobby')
    print(username)
    print(hobbys)
    return HttpResponse('')


def form_get_handler(request):
    username = request.GET.get('username')
    hobbys = request.GET.getlist('hobby')
    print(username)
    print(hobbys)
    return HttpResponse('')


def register_handler(request):
    if request.method == 'GET':
        return render(request, './register.html')
    else:
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = User()
        user.username = username
        user.password = password
        user.save()
        print(username,password)
        return HttpResponse('success')